define([
    "dojo/_base/declare",
    "gistech/widgets/normalDialog/NormalDialog",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/text!./templates/template.html",
    "dojo/dom-construct",
    "dojo/on","dojo/topic",
    "dojo/_base/connect",
    "gistech/util/ajaxUtil",
    "gistech/widgets/drawGeometryWidget/DrawGeometryWidget",
    "esri/graphic",
    "esri/geometry/geometryEngine",
    "esri/geometry/geodesicUtils",
    "esri/geometry/Polyline",
    "esri/toolbars/edit",
    "dojo/_base/event",
    "esri/geometry/webMercatorUtils",
    "esri/tasks/GeometryService",
    "esri/layers/FeatureLayer","esri/InfoTemplate",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/Color",
    "dojo/dom-style","dojo/query","dojo/dom-class"],
    function(declare,NormalDialog,lang,style,template,domConstruct,
    		on,topic,eventconnect,ajaxUtil,DrawGeometryWidget,Graphic,geometryEngine,geodesicUtils,Polyline,Edit,event,webMercatorUtils,GeometryService,FeatureLayer,
    		InfoTemplate,SimpleMarkerSymbol,SimpleLineSymbol,SimpleFillSymbol,Color,
    		domStyle,query,domClass){
        return declare([NormalDialog],{
            templateString:template, 
            LandNoteLayer:null,//备注图层
            handles:[],
            constructor:function (args) {
            	this.inherited(arguments);
            	this.map = args.map;
                this.LandNoteLayer=null;
            },
            postCreate:function(){
                this.inherited(arguments);
            	on(this.drowLand,"click",lang.hitch(this,this.createDrawbarWidget));
                on(this.save, "click",lang.hitch(this,this.saveLandGeometryToDB));
                on(this.cancle,"click",lang.hitch(this,this.closeDialog));
            },
            startup:function(){
                this.inherited(arguments);    
                this.getThisLayer();
                this.init();
                this.addEvents();
            },
            init:function(){
            	this.inherited(arguments); 
            },
            addEvents:function(){
            	on(this.LandNoteLayer,"before-apply-edits",lang.hitch(this,function(){
            		try{this.map.graphics.clear();}finally{return ;}}));
            	topic.subscribe("landNoteDrawed",lang.hitch(this,this.fullLandExpandAttribute));
            },
            fullLandExpandAttribute:function(args){
            	debugger
            	this.landgeo = args.geo;
            },
            createDrawbarWidget:function(){
            	debugger;
            	if(dijit.byId("LandNoteDraw")){
            		return false;
            	}else{
            		Global.currentDraw="landNote";
            		var drawGeometryWidget = new DrawGeometryWidget({map:this.map,id:"LandNoteDraw"});
                	drawGeometryWidget.startup();
            	}},
            destroy:function(){
    			this.inherited(arguments);
    			this.map = null;
    		},
            getThisLayer:function(){
//            	this.LandNoteLayer=this.map.getLayer("LANDNOTE");
            	this.LandNoteLayer=new FeatureLayer(mapinfoTemplate.LANDNOTE.layerurl);
            },
            closeDialog:function(){
            	debugger
    			this.inherited("closeDialog",arguments);
    			this.destroy();
    			topic.publish("LandNoteWidgetDestroy");
    			this.LandNoteLayer.refresh();
    			dojo.forEach(this.handles,function(handle){
    				handle.remove();
    			})
    		},
            saveLandGeometryToDB:function(){
            	debugger;
            	if(this.landgeo==undefined){
            		layer.msg("请确保完成了边界绘制!",{icon:2,time:1000})
            		return false;
            	}
				this.landgeo.attributes={
						LANDTYPE:dojo.query("#LANDTYPE")[0].value, 
						NOTE:dojo.query("#landNote")[0].value,
						NOTEUSERNAME:userInfoData.USERNAME,
						NOTEDATE:formatDate(new Date()) 
				};
				console.log(this.landgeo.attributes);
				var self=this;
				this.LandNoteLayer.applyEdits([this.landgeo],null,null,
						function(evt){layer.close(index); layer.msg("备注成功!",{icon:1});self.closeDialog()},
						function(evt){layer.close(index); layer.msg("备注失败!",{icon:2})});
				var index=layer.load(1);
            },
    });
});

